package Datebase;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class tscoreluru extends JFrame {
    public static void main(String[] args) {
        System.setProperty("sun.java2d.noddraw", "true");
        new tscoreluru("TNO");
    }

    public tscoreluru(String tno){
        JFrame mod = new JFrame("学生成绩录入系统");
        URL resource = tscoreluru.class.getClassLoader().getResource("kun.png");
        Image image = new ImageIcon(resource).getImage();
        mod.setIconImage(image);
        mod.setSize(800,600);
        mod.setLocationRelativeTo(null);

        JLabel tot = new JLabel("教授的课程如下:");
        tot.setFont(new Font("宋体", Font.PLAIN, 20));
        tot.setBounds(20,0,350,55);
        mod.add(tot);
        mod.setLayout(null);

        try{
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String dbURL = "jdbc:oracle:thin:@localhost:1521:XE";
            String user = "HR";
            String password = "root";
            Connection conn = DriverManager.getConnection(dbURL, user, password);
            String sql = "SELECT TEACH.TNO,COURSE.CNO,CNAME,CCREDIT,TERM FROM TEACH,COURSE WHERE TEACH.CNO=COURSE.CNO AND TEACH.TNO='"+tno+"'";

            PreparedStatement pstm = conn.prepareStatement(sql);
            ResultSet rs = pstm.executeQuery();

            int count = 0;
            while (rs.next()) {
                count++;
            }
            rs = pstm.executeQuery();

            Object[][] data = new Object[count][5];
            String[] title = {"教工号","课程号","课程名","学分","学期"};
            count = 0;
            while (rs.next()) {

                data[count][0] = (rs.getString("TNO"));
                data[count][1] = (rs.getString("CNO"));
                data[count][2] = (rs.getString("CNAME"));
                data[count][3] = (rs.getString("CCREDIT"));
                data[count][4] = (rs.getString("TERM"));
                count++;
            }
            JTable table = new JTable(data, title);
            JScrollPane scrollPane = new JScrollPane(table);
            DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
            cr.setHorizontalAlignment(SwingConstants.CENTER);
            table.setDefaultRenderer(Object.class,cr);

            table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
            table.getColumnModel().getColumn(0).setPreferredWidth(200);
            table.getColumnModel().getColumn(1).setPreferredWidth(200);
            table.getColumnModel().getColumn(2).setPreferredWidth(200);
            table.getColumnModel().getColumn(3).setPreferredWidth(200);
            table.getColumnModel().getColumn(4).setPreferredWidth(200);
            table.setRowHeight(80);
            table.setFont(new Font("微软雅黑",Font.BOLD,13));
            scrollPane.setBounds(90, 50, 600, 185);
            mod.add(scrollPane);

        }catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
            JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE);
        }catch (SQLException sqle) {
            sqle.printStackTrace();
            JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
        }


        JLabel tot1 = new JLabel("请输入待录入成绩的课程号:");
        tot1.setFont(new Font("宋体", Font.PLAIN, 20));
        tot1.setBounds(60,270,400,55);
        mod.add(tot1);
        JTextField textField1 = new JTextField();
        textField1.setFont(new Font("微软雅黑",Font.PLAIN,18));
        textField1.setColumns(20);
        textField1.setBounds(330,285,150,25);
        mod.add(textField1);

        JButton button1=new JButton("确认");
        button1.setBounds(500,285,80,25);
        button1.setFont(new Font("微软雅黑",Font.PLAIN,18));
        mod.add(button1);


        JLabel tot2 = new JLabel("学号:");
        tot2.setFont(new Font("宋体", Font.PLAIN, 18));
        tot2.setBounds(305,360,400,55);

        JTextField textField2 = new JTextField();
        textField2.setFont(new Font("微软雅黑",Font.PLAIN,16));
        textField2.setColumns(20);
        textField2.setBounds(365,375,100,25);


        JLabel tot3 = new JLabel("成绩:");
        tot3.setFont(new Font("宋体", Font.PLAIN, 18));
        tot3.setBounds(305,420,400,55);

        JTextField textField3 = new JTextField();
        textField3.setFont(new Font("微软雅黑",Font.PLAIN,16));
        textField3.setColumns(20);
        textField3.setBounds(365,435,100,25);


        JButton button2=new JButton("录入");
        button2.setBounds(345,500,80,25);
        button2.setFont(new Font("微软雅黑",Font.PLAIN,18));

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("oracle.jdbc.driver.OracleDriver");
                    String dbURL1 = "jdbc:oracle:thin:@localhost:1521:XE";
                    String user1 = "HR";
                    String password1 = "root";
                    Connection conn1 = DriverManager.getConnection(dbURL1, user1, password1);
                    String sql1 = "SELECT CNO FROM EXAM WHERE CNO = '"+textField1.getText()+"'";

                    PreparedStatement pstm1 = conn1.prepareStatement(sql1);
                    ResultSet rs1 = pstm1.executeQuery();

                    int count1 = 0;
                    while (rs1.next()) {
                        count1++;
                    }

                    if(count1 == 0)
                    {
                        JOptionPane.showMessageDialog(null, "课程号不存在!",
                                "提示", JOptionPane.WARNING_MESSAGE);
                    }
                    else{
                        mod.repaint();
                        mod.add(tot2);
                        mod.add(textField2);
                        mod.add(tot3);
                        mod.add(textField3);
                        mod.add(button2);

                        button2.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                try {
                                    Class.forName("oracle.jdbc.driver.OracleDriver");
                                    String dbURL = "jdbc:oracle:thin:@localhost:1521:XE";
                                    String user = "HR";
                                    String password = "root";
                                    Connection conn = DriverManager.getConnection(dbURL, user, password);
                                    String sql = "INSERT INTO EXAM (SNO,CNO,GRADE) values ('" + textField2.getText()+ "','" + textField1.getText() + "','" + textField3.getText() + "')";

                                    PreparedStatement pstm = conn.prepareStatement(sql);
                                    ResultSet rs = pstm.executeQuery();

                                    JOptionPane.showMessageDialog(null, "录入成功！","录入状态", JOptionPane.INFORMATION_MESSAGE);
                                } catch (ClassNotFoundException cnfe) {
                                    cnfe.printStackTrace();
                                    JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE);
                                } catch (SQLException sqle) {
                                    sqle.printStackTrace();
                                    JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
                                }
                            }
                        });
                    }
                } catch (ClassNotFoundException cnfe) {
                    cnfe.printStackTrace();
                    JOptionPane.showMessageDialog(null, "该学生成绩已存在！", "错误", JOptionPane.ERROR_MESSAGE);
                } catch (SQLException sqle) {
                    sqle.printStackTrace();
                    JOptionPane.showMessageDialog(null, "该学生成绩已存在！", "错误", JOptionPane.ERROR_MESSAGE);
                }

            }
        });
        mod.setVisible(true);

    }
}
